import os
import shutil
import pandas as pd
# from . import utils
# from . import utils

# CSV_PATH = "..\\"
CSV_PATH = ".\\"
ROOT_PATH = "F:\\Download\\mav\\hanime\\"
# ROOT_PATH = "D:\\Download\\mav\\"

# 检查档案名称
def check_filename(name):
    name = (str(name) or "").replace("?", "")
    name = name.replace("|", "")
    name = name.replace("\\", "")
    name = name.replace("/", "")
    name = name.replace(".", "")
    name = name.replace(":", "")
    name = name.replace(">", "")
    name = name.replace("<", "")
    name = name.replace('"', "")
    name = name.replace('*', "")
    return name

# 取的目录下的档案数量
def file_count(directory):
    count = 0
    for file_name in os.listdir(directory):
        file_path = os.path.join(directory, file_name)
        if os.path.isfile(file_path):
            count += 1
    return count

# 將 JSON 轉換為 CSV
def Json2CSV():
    topic = "comics"
    author = "_other"
    #author = "Webtoon"
    json_file = f'{CSV_PATH}{topic}.json'
    csv_file = f'{CSV_PATH}{topic}.csv'
    #print("csv_file=", csv_file)
    df = pd.read_json(json_file)
    df = df.drop_duplicates()
    df = df.loc[:, ['id', 'pages', 'galleries', 'title']]
    # 显示前几行数据
    #print(df.head())
    with open(csv_file, 'w+', encoding="utf-8") as fp:
        for row in df.itertuples():
            id = str(row.id)
            pages = int(row.pages)
            galleries = str(row.galleries)
            title = str(row.title)
            title = title.replace("\r", "")
            title = title.replace("\n", "")
            image_url = f'https://i.nhentai.net/galleries/{galleries}/1.jpg'
            #if self.debug_mode:
            line_data = f'{id},{pages},{image_url},{title}\r'
            print(line_data)
            fp.writelines(line_data)


# 檢查下載的檔案是否有缺失
def CheckFiles(base_path=""):
    if base_path == "":
        base_path = CSV_PATH
    topic = "comics"
    author = "_other"
    #author = "Webtoon"
    csv_file = f'{base_path}{topic}.csv'
    df = pd.read_csv(csv_file)
    df = df.drop_duplicates()
    df = df.loc[:, ['id', 'pages', 'author', 'image_url', 'title']]
    # print(df.head())
    print("CheckFiles :")
    # 显示前几行数据
    for row in df.itertuples():
        id = str(row.id)
        pages = int(row.pages)
        author = str(row.author)
        author = check_filename(author)
        image_url = str(row.image_url)
        title = str(row.title)
        title = title.replace("\r", "")
        title = title.replace("\n", "")
        folder = f'{ROOT_PATH}{topic}\\{author}\\{id}'
        total = file_count(folder)
        if total < pages:
            print(f'{id} : {total} < {pages}')

#Main Run Thread
if __name__=='__main__':
    #print("CheckFiles :")
    # python tools.py
    #topic = "3D"
    #topic = "comics"
    #Json2CSV()
    CheckFiles()
    
